#include "stdlib.h"
#include "stdint.h"
#include <string.h>
#include "math.h"

#include "..\..\BSP\bsp_gpio.h"
#include "..\..\BSP\bsp_pwm.h"
#include "..\..\BSP\bsp_uart.h"
#include "..\..\BSP\bsp_nvic.h"
#include "..\..\BSP\bsp_exit.h"
#include "..\..\BSP\bsp_lvr.h"
#include "..\..\BSP\bsp_wdt.h"
#include "..\..\BSP\bsp_timer.h"
#include "..\..\BSP\delay.h"
#include "bsp_BL5372.h"

#define ADD_READ 	     0x65
#define ADD_WRITE        0x64

#define TIME_12          0
#define TIME_24          1


#define SDA_OUT()     //sda线输出
#define SDA_IN()       //sda线输出

#define IIC_SCL_SET    	GPIO_WriteBit(GPIOB, GPIO_Pin_12, Bit_SET)		// SCL IO高电平  // SCL IO
#define IIC_SCL_RESET   GPIO_WriteBit(GPIOB, GPIO_Pin_12, Bit_RESET)	// SCL IO低电平   // SCL IO
#define IIC_SDA_SET     GPIO_WriteBit(GPIOB, GPIO_Pin_9, Bit_SET)		// SDA IO高电平  // SDA IO
#define IIC_SDA_RESET   GPIO_WriteBit(GPIOB, GPIO_Pin_9, Bit_RESET)	// SDA IO低电平   // SDA IO

#define IIC_READ_SDA 	LL_GPIO_IsInputPinSet()


#define IIC_READ_SDA 	LL_GPIO_IsInputPinSet()



static uint8_t  LL_GPIO_IsInputPinSet(void)
{
	if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_9) == 1) 
	{
		return 1;
	}
	else 
	{
		return 0;
	}
} 


 void my_iic_init(void)
{
 IIC_SCL_SET    ;

 IIC_SDA_SET  ;
	
}

static void my_iic_start(void)
{
    SDA_OUT();     //sda线输出
    IIC_SDA_SET;
    IIC_SCL_SET;
    rt_hw_us_delay(20);
    IIC_SDA_RESET;//START:when CLK is high,DATA change form high to low
    rt_hw_us_delay(20);
    IIC_SCL_RESET;//钳住I2C总线，准备发送或接收数据
}

static void my_iic_stop(void)
{
    SDA_OUT();//sda线输出
    IIC_SCL_RESET;
    IIC_SDA_RESET;//STOP:when CLK is high DATA change form low to high
    rt_hw_us_delay(20);
    IIC_SCL_SET;
    rt_hw_us_delay(20);
    IIC_SDA_SET;//发送I2C总线结束信号
}

static uint8_t my_iic_wait_ack(void)
{
    uint8_t ucErrTime = 0;
    SDA_IN();      //SDA设置为输入
    IIC_SDA_SET; rt_hw_us_delay(20);
    IIC_SCL_SET; rt_hw_us_delay(20);
    while (IIC_READ_SDA)
    {
        ucErrTime++;
				rt_hw_us_delay(1);
        if (ucErrTime > 250)
        {
            my_iic_stop();
            return 1;
        }
    }
    IIC_SCL_RESET;//时钟输出0
    return 0;
}

static void my_iic_ack(void)
{
    IIC_SCL_RESET;
    SDA_OUT();
    IIC_SDA_RESET;
    rt_hw_us_delay(20);
    IIC_SCL_SET;
    rt_hw_us_delay(20);
    IIC_SCL_RESET;
}
//不产生ACK应答
static void my_iic_nack(void)
{
    IIC_SCL_RESET;
    SDA_OUT();
    IIC_SDA_SET;
    rt_hw_us_delay(20);
    IIC_SCL_SET;
    rt_hw_us_delay(20);
    IIC_SCL_RESET;
}


//IIC发送一个字节
//返回从机有无应答
//1，有应答
//0，无应答
static void my_iic_send_byte(uint8_t txd)
{
    uint8_t t;
    SDA_OUT();
    IIC_SCL_RESET;//拉低时钟开始数据传输
    for (t = 0; t < 8; t++)
    {
        if ((txd & 0x80) >> 7)
        {
            IIC_SDA_SET;
        }
        else
            IIC_SDA_RESET;
        txd <<= 1;
        rt_hw_us_delay(20);   //对TEA5767这三个延时都是必须的
        IIC_SCL_SET;
        rt_hw_us_delay(20);
        IIC_SCL_RESET;
        // rt_hw_us_delay(20);
    }
}
//读1个字节，ack=1时，发送ACK，ack=0，发送nACK
static uint8_t my_iic_read_byte(uint8_t ack)
{
    unsigned char i, receive = 0;
	
	IIC_SDA_SET;  //ARM内核
    SDA_IN();//SDA设置为输入
	rt_hw_us_delay(20);
    for (i = 0; i < 8; i++ )
    {
        IIC_SCL_RESET;
        rt_hw_us_delay(20);
        IIC_SCL_SET;
        receive <<= 1;
        if (IIC_READ_SDA)receive++;
        rt_hw_us_delay(20);
    }
    if (!ack)
        my_iic_nack();//发送nACK
    else
        my_iic_ack(); //发送ACK
    return receive;
}

/***********************************BL7253时钟芯片代码**********************************************/

static uint8_t BCDtoHex(uint8_t BCD)
{
	return ((BCD>>4)*10+(BCD&0x0f));
}

static uint8_t HextoBCD(uint8_t Hex)
{
	return  (((Hex/10)<<4)+Hex%10);
}


static void BL5732_read_bytes(uint8_t addr, uint8_t DATA[], uint8_t num)
{
	int i = 0;
	my_iic_start();

	my_iic_send_byte(ADD_WRITE);
	my_iic_wait_ack();
	
	
	my_iic_send_byte(addr |0x04 ); //发送低地址
	my_iic_wait_ack();

	for (i = 0; i < num - 1; i++)
	{
		DATA[i] = my_iic_read_byte(1);
	}
	DATA[i] = my_iic_read_byte(0);
	my_iic_stop();

}


static void BL5732_write_bytes(uint8_t addr, uint8_t DATA[], uint8_t num)
{
	uint8_t i =0;
	
	my_iic_start();

	my_iic_send_byte(ADD_WRITE);
	
	my_iic_wait_ack();
	
	my_iic_send_byte(addr); 
	
	my_iic_wait_ack();
	
	for (i = 0; i < num; i++)
	{
		my_iic_send_byte(*(DATA + i)); //发送字节
		my_iic_wait_ack();
	}
	my_iic_stop(); //产生一个停止条件

}


uint8_t  read_BL7253time(uint8_t *Time)
{
	uint8_t data_1[7] ={0};
	uint8_t data_2[7] ={0};
	
	BL5732_read_bytes(0x00,data_1, 7);
	BL5732_read_bytes(0x00,data_2, 7);
	
	if(memcmp(&data_1[1], &data_2[1], sizeof(data_1)-1) == 0)
	{
		
		Time[0] =BCDtoHex(data_1[0]);
		Time[1] =BCDtoHex(data_1[1]);
		Time[2] =BCDtoHex(data_1[2]);
		
		 return 1;	
	}
	else
	{
      return 0;	
	}
}

void  read_BL7253_XSTP(uint8_t *pData)
{
	uint8_t Ctrl_7;
	
	BL5732_read_bytes(0xF0,&Ctrl_7, 1);

	*pData =Ctrl_7;
	
}


uint8_t  wirte_BL7253time(uint8_t* Time)
{
	uint8_t Date[7] ={0};
	
	Date[0] =HextoBCD(Time[0]);
	Date[1] =HextoBCD(Time[1]);
	Date[2] =HextoBCD(Time[2]);
	
	BL5732_write_bytes(0x00,Date, 7);
	return 1;
}


void	wirte_BL7253_12_24(uint8_t AM)
{
	uint8_t Ctrl_7;

	//BL5732_read_bytes(0xF0,&Ctrl_7, 1);
	
	if(AM ==TIME_12)
	{
		Ctrl_7 =Ctrl_7&0x3F;
	}
	else
	{
		Ctrl_7 =Ctrl_7|0x20;
	}
	BL5732_write_bytes(0xF0,&Ctrl_7, 1);
}




